package common

import (
	"strings"
)

type ArrayT interface {
	byte | int8 | int16 | int32 | int64 | int | uint16 | uint32 | uint64 | uint | string
}

// StringToUint64Array 字符串转uint64数组
func StringToUint64Array(str string, sub string) []uint64 {
	if str == "0" || str == "" {
		return make([]uint64, 0)
	}
	str = strings.ReplaceAll(str, " ", "")
	arr := strings.Split(str, sub)
	result := make([]uint64, len(arr))
	for i, v := range arr {
		result[i], _ = NewString(v).ToUint64()
	}
	return result
}

// StringToIntArray 字符串转int数组
func StringToIntArray(str string, sub string) []int {
	if str == "0" || str == "" {
		return make([]int, 0)
	}
	str = strings.ReplaceAll(str, " ", "")
	arr := strings.Split(str, sub)
	result := make([]int, len(arr))
	for i, v := range arr {
		result[i], _ = NewString(v).ToInt()
	}
	return result
}

func StringToInt32Array(str string, sub string) []int32 {
	if str == "0" || str == "" {
		return make([]int32, 0)
	}
	str = strings.ReplaceAll(str, " ", "")
	arr := strings.Split(str, sub)
	result := make([]int32, len(arr))
	for i, v := range arr {
		tmp, _ := NewString(v).ToInt()
		result[i] = int32(tmp)
	}
	return result
}

func StringToInt64Array(str string, sub string) []int64 {
	if str == "0" || str == "" {
		return make([]int64, 0)
	}
	str = strings.ReplaceAll(str, " ", "")
	arr := strings.Split(str, sub)
	result := make([]int64, len(arr))
	for i, v := range arr {
		tmp, _ := NewString(v).ToInt()
		result[i] = int64(tmp)
	}
	return result
}

// IntArrayToString []int 转string
func IntArrayToString[T ArrayT](arr []T, sub string) string {
	sb := NewStringBuilder()
	for i, v := range arr {
		if i != 0 {
			sb.Append(sub)
		}
		sb.Append(NewStringAny(v).ToString())
	}
	return sb.ToString()
}

func ArrayToString(arr []interface{}, sub string) string {
	sb := NewStringBuilder()
	for i, v := range arr {
		if i != 0 {
			sb.Append(sub)
		}
		sb.Append(NewStringAny(v).ToString())
	}
	return sb.ToString()
}

// ArrayIndexof 数组查找，返回索引，-1为没找到
func ArrayIndexof[T ArrayT](array []T, v T) int {
	for i, m := range array {
		if m == v {
			return i
		}
	}
	return -1
}

// //ArrayIndexof 数组查找，返回索引，-1为没找到
// func Arrayuint8Indexof(array []uint8, v uint8) int {
// 	for i, m := range array {
// 		if m == v {
// 			return i
// 		}
// 	}
// 	return -1
// }

// //ArrayIndexof 数组查找，返回索引，-1为没找到
// func Arrayint32Indexof(array []int32, v int32) int {
// 	for i, m := range array {
// 		if m == v {
// 			return i
// 		}
// 	}
// 	return -1
// }

// //ArrayIndexof 数组查找，返回索引，-1为没找到
// func Arrayuint64Indexof(array []uint64, v uint64) int {
// 	for i, m := range array {
// 		if m == v {
// 			return i
// 		}
// 	}
// 	return -1
// }

// 在arr1中找arr2的成员，只要找到一个就返回ture
func ArraySelectArray[T ArrayT](arr1, arr2 []T) bool {
	for i := range arr2 {
		if ArrayIndexof(arr1, arr2[i]) >= 0 {
			return true
		}
	}
	return false
}
